package com.restkeeper.operator.controller;

import com.restkeeper.operator.entity.EnterpriseAccount;
import com.restkeeper.operator.service.IEnterpriseAccountService;
import com.restkeeper.operator.vo.AddEnterpriseAccountVO;
import com.restkeeper.operator.vo.ResetPwdVO;
import com.restkeeper.operator.vo.UpdateEnterpriseAccountVO;
import com.restkeeper.response.vo.PageVO;
import com.restkeeper.utils.AccountStatus;
import com.restkeeper.utils.Result;
import com.restkeeper.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Api(tags = {"企业帐号管理"})
@RestController
@RequestMapping("/enterprise")
@Slf4j

public class EnterpriseAccountController {
    @Reference(version = "1.0.0",check = false)
    private IEnterpriseAccountService iEnterpriseAccountService;

    @ApiOperation("查询企业列表")
    @GetMapping("/pageList/{page}/{pageSize}")
    public PageVO<EnterpriseAccount> findListByPage(@PathVariable("page") int page,
                                                    @PathVariable("pageSize") int pageSize,
                                                    @RequestParam(value = "enterpriseName",required = false) String enterpriseName ){
        return new PageVO<EnterpriseAccount>(iEnterpriseAccountService.queryPageByName(page,pageSize,enterpriseName));
    }

    @ApiOperation("新增账号")
    @PostMapping("/add")
    public boolean add(@RequestBody AddEnterpriseAccountVO addEnterpriseAccountVO){
        //bean拷贝
        EnterpriseAccount enterpriseAccount = new EnterpriseAccount();
        BeanUtils.copyProperties(addEnterpriseAccountVO,enterpriseAccount);

        //设置时间
        LocalDateTime localDateTime = LocalDateTime.now();
        enterpriseAccount.setApplicationTime(localDateTime);

        //设置过期时间
        LocalDateTime expireTime = null;
        //试用账号时间为7天
        if (enterpriseAccount.getStatus() == 0) {
            expireTime = localDateTime.plusDays(7);
        }
        if (enterpriseAccount.getStatus() == 1){
            expireTime = localDateTime.plusDays(addEnterpriseAccountVO.getValidityDay());
        }
        if(expireTime != null){
            enterpriseAccount.setExpireTime(expireTime);
        }else {
            throw new RuntimeException("帐号类型信息设置有误");
        }
        return iEnterpriseAccountService.add(enterpriseAccount);
    }

    //根据id查询账号信息
    @ApiOperation("账号查询")
    @GetMapping(value = "/getById/{id}")
    public EnterpriseAccount getById(@PathVariable("id") String id){
        return iEnterpriseAccountService.getById(id);
    }

    //账号编辑
    @ApiOperation("账号编辑")
    @PutMapping("/update")
    public Result update(@RequestBody UpdateEnterpriseAccountVO updateEnterpriseAccountVO) {
        Result result = new Result();
        //查询账号原有信息
        String enterpriseId = updateEnterpriseAccountVO.getEnterpriseId();
        EnterpriseAccount enterpriseAccount = iEnterpriseAccountService.getById(enterpriseId);
        if (enterpriseAccount == null) {
            result.setStatus(ResultCode.error);
            result.setDesc("修改账号不存在");
            return result;
        }
        //修改校验状态
        if (updateEnterpriseAccountVO.getStatus() != null) {
            if (updateEnterpriseAccountVO.getStatus() == 0 && enterpriseAccount.getStatus() == 1) {
                result.setStatus(ResultCode.error);
                result.setDesc("不能将正式帐号改为试用帐号");
                return result;
            }
            //试用改正式
            if (updateEnterpriseAccountVO.getStatus() == 1 && enterpriseAccount.getStatus() == 0) {
                //获取到期时间
                LocalDateTime localDateTime = LocalDateTime.now();
                LocalDateTime expireTime = localDateTime.plusDays(updateEnterpriseAccountVO.getValidityDay());
                enterpriseAccount.setApplicationTime(localDateTime);
                enterpriseAccount.setExpireTime(expireTime);
            }
            //正式改延期
            if (updateEnterpriseAccountVO.getStatus() == 1 && enterpriseAccount.getStatus() == 1) {
                LocalDateTime now = LocalDateTime.now();
                //到期时间
                LocalDateTime expireTime = now.plusDays(updateEnterpriseAccountVO.getValidityDay());
                enterpriseAccount.setExpireTime(expireTime);
            }
        }
        //其他字段设置
        BeanUtils.copyProperties(updateEnterpriseAccountVO, enterpriseAccount);

        //执行修改
        boolean flag = iEnterpriseAccountService.updateById(enterpriseAccount);
        if (flag) {
            //修改成功
            result.setStatus(ResultCode.success);
            result.setDesc("修改成功");
            return result;
        } else {
            //修改失败
            result.setStatus(ResultCode.error);
            result.setDesc("修改失败");
            return result;
        }
    }

    //账号删除
    @ApiOperation("账号删除")
    @DeleteMapping("/deleteById/{id}")
    public boolean deleteById(@PathVariable("id") String id){
        return iEnterpriseAccountService.removeById(id);
    }

    //账号还原
    @ApiOperation("账号还原")
    @PutMapping("/recovery/{id}")
    public boolean recovery(@PathVariable("id") String id){
        return iEnterpriseAccountService.recovery(id);
    }

    //账号禁用
    @ApiOperation("账号禁用")
    @PutMapping("/forbidden/{id}")
    public boolean forbidden(@PathVariable("id") String id){
        EnterpriseAccount enterpriseAccount = iEnterpriseAccountService.getById(id);
        enterpriseAccount.setStatus(AccountStatus.Forbidden.getStatus());
        return iEnterpriseAccountService.updateById(enterpriseAccount);
    }

    //重置密码
    @ApiOperation("重置密码")
    @PutMapping("/resetPwd")
    public boolean resetPwd(@RequestBody ResetPwdVO resetPwdVO){
        return iEnterpriseAccountService.resetPwd(resetPwdVO.getId(),resetPwdVO.getPwd());
    }
}
